package cn.edu.zut.hhr.service.impl;

import cn.edu.zut.hhr.entity.Tag;
import cn.edu.zut.hhr.mapper.TagMapper;
import cn.edu.zut.hhr.service.TagService;
import cn.edu.zut.hhr.vo.RestResponse;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Many;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 标签表 服务实现类
 * </p>
 *
 * @author HHR
 * @since 2021-08-21
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    @Resource
    private TagMapper tagMapper;

    /**
     * 通过文章id获取标签
     * @param id
     * @return
     */
    @Override
    public List<Tag> getByAId(Long id) {
        return tagMapper.getByAid(id);
    }

    @Override
    public List<Tag> getHotTags(int count) {
        List<Integer> tagIds = tagMapper.getHotTagsId(count);
        if(tagIds == null || tagIds.size() == 0){
            return Collections.<Tag>emptyList();
        }
        List<Tag> tags = tagMapper.getTagsById(tagIds);
        return tags;
    }

    @Override
    public RestResponse getAllTags() {
        return RestResponse.success(tagMapper.selectList(Wrappers.lambdaQuery()));
    }

    @Override
    public void insertMany(List<Tag> tags) {
        tagMapper.insertMany(tags);
    }

}
